#include <glib.h>
#include <stdio.h>
#include <unistd.h>



#define NUM_THREADS 5



void thread_func(gpointer data, gpointer user_data){
    int *num = (int *)data;
    printf("Thread %d is running\n", *num);
    sleep(*num);

    printf("Thread %d is finished\n", *num);
}
int main(int argc, char const *argv[])
{   
    //创建线程池
    GThreadPool *pool = g_thread_pool_new(NULthread_func, NULL, NUM_THREADS, FALSE, NULL);
    
    //向线程池中添加任务
    for (int i = 0; i < 10; i++)
    {
        
        g_thread_pool_push(pool, &i, NULL);
    }

    //等待线程池中的任务完成
    g_thread_pool_free(pool, FALSE, TRUE);

    printf("All threads have finished\n");
    
    /* code */
    return 0;
}
